﻿@page
@model IndexModel
@{
    ViewData["Title"] = "登录";
}

<header class='notice-header'>
    <h1 class="notice-title">登 录</h1>
</header>
<div class="weui-cells weui-cells_form">
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label required">账号</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input " id="account" type="text" pattern="[0-9a-zA-Z]*" placeholder="请输入账号">
        </div>
    </div>

    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label required">密码</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input " id="pwd" type="password" value="">
        </div>
    </div>

    <div class="weui-cell weui-cell_vcode">
        <div class="weui-cell__hd"><label class="weui-label required">验证码</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="code" type="number" placeholder="请输入验证码">
        </div>
        <div class="weui-cell__ft">
            <img class="weui-vcode-img" src="" onclick="clickimg()">
        </div>
    </div>
    <label for="weuiAgree" class="weui-agree">
        <input id="weuiAgree" type="checkbox" class="weui-agree__checkbox">
        <span class="weui-agree__text">
            记住用户名
        </span>
    </label>
    <div class="weui-btn-area">
        <a class="weui-btn weui-btn_primary" href="javascript:" id="LoginBtn">确 定</a>
    </div>
</div>
@section Scripts{
    <script type="text/javascript">
        var yzm_id = "";
        clickimg();
        var account = getCookie("account");
        if (IsVaild(account)){
            $("#account").val(account);
            $("#weuiAgree").prop("checked", true);
        }
        function clickimg() {
            ajax({
                url: "/VerifyCode/NumberVerifyCode",
                contentType: "application/x-www-form-urlencoded",
                dataType: "text",
                success: function (data) {
                    var ss = data.split('###');
                    $(".weui-vcode-img").attr('src', ss[0]);
                    yzm_id = ss[1];
                }
            });
        }

        $("#LoginBtn").click(function () {
            if (IsNullOrEmpty($("#account").val())) {
                $.toptip('账号不为空', 'error');
                return;
            }
            if (IsNullOrEmpty($("#pwd").val())) {
                $.toptip('密码不为空', 'error');
                return;
            }
            if (IsNullOrEmpty($("#code").val())) {
                $.toptip('验证码不为空', 'error');
                return;
            }
            ajax({
                url: API_Address + "/User/Login",
                data: {
                    Account: $("#account").val(),
                    Pwd: $("#pwd").val(),
                    code: $("#code").val(),
                    codeId: yzm_id
                },
                success: function (data) {
                    if (data.type == 404) {
                        $.toptip(data.message, 'error');
                    }
                    else if (data.type == 200) {
                        $.toptip(data.message, 'success');
                        
                        if ($("#weuiAgree").prop("checked")) {
                            setCookie("account", $("#account").val(), 30);
                        }
                        var token = data.data.accessToken.tokenContent;
                        localStorage.setItem("API_Bearer", "Bearer " + token);
                        localStorage.setItem("Token_Expired", data.data.accessToken.expires);
                        window.location.href = '/task'
                    }
                }
            });

        });
    </script>
}